package printer.impl;

import java.text.SimpleDateFormat;
import java.util.Set;

import pojo.Customer;
import printer.DataPrinter;

public class DataPrinterImpl implements DataPrinter {
   
	public StringBuilder print(Set<Customer> customer, String format) {
		
		if(format.equals("XML"))
			return this.printXml(customer); 
		else if(format.equals("I/B"))
		{
			return this.printIB(customer); 
		}
		else if(format.equals("HELP"))
		{
			return this.printHelp();
		}
		else
			return this.printHelp();

	}
	
	StringBuilder printIB(Set<Customer> customer) {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder("");
		for(Customer cus:customer)
		{
			sb.append(cus.toString()+"\n\r");
		}
		return sb;
	}

	public static StringBuilder printHelp() {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder("");
		sb.append("Read this Help Document\n\r");
		sb.append("Input these command you kan get:\n\r");
		sb.append("L ID	 Print customer information in the order of Id\n\r");
		sb.append("L BIRTH	 Print customer information in the order of Birthday\n\r");
		sb.append("L XML    Print customer information in XMLformat\n\r");
		sb.append("HELP     Get HELP Doucment\n\r");
		sb.append("Q        Stop system!");
		return sb;
	}

	StringBuilder printXml(Set<Customer> customer)
	{
		StringBuilder sb=new StringBuilder("");
		sb.append("<Customers>\n\r");
		for(Customer cus:customer)
		{
			sb.append(" <Customer>\n\r");
			sb.append("	  <Customer id= "+cus.getId()+" >\n\r");
			sb.append("     <name>"+cus.getName()+"</name>\n\r");
			sb.append("     <sex>"+cus.isSex()+"</sex>\n\r");
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			sb.append("     <birthday>"+format.format(cus.getBirth())+"</birthday>\n\r");
			sb.append("     <email>"+cus.getEmail()+"</email>\n\r");
			sb.append(" </Customer>\n\r");
		}
		sb.append("</Customers>\n\r");
		return sb;
		
	}

}
